package com.cw.demo.dao.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.cw.demo.model.Emp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Repository
public class DaoEmpImpl implements DaoEmp {
@Autowired
    DruidDataSource dataSource;
//    public boolean saveDept(Emp emp){
//
//        try {
//            Connection connection = dataSource.getConnection();
//            PreparedStatement preparedStatement = connection.prepareStatement("insert into dept(id, dname, loc) values (?,?,?)");
//            preparedStatement.setInt(1,dept.getId());
//            preparedStatement.setString(2,dept.getdName());
//            preparedStatement.setString(3,dept.getLoc());
//            preparedStatement.execute();
//            return true;
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return false;
//        }

//    }

//    public boolean updateDept(Dept dept){
//
//
//        try {
//            Connection connection = dataSource.getConnection();
//            PreparedStatement preparedStatement = connection.prepareStatement("update dept set dname = ? , loc = ? where  id = ?");
//            preparedStatement.setString(1,dept.getdName());
//            preparedStatement.setString(2,dept.getLoc());
//            preparedStatement.setInt(3,dept.getId());
//            return true;
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return false;
//        }
//    }

//    public boolean deleteDept(int id){
//
//        try {
//            Connection connection = dataSource.getConnection();
//            PreparedStatement preparedStatement = connection.prepareStatement("delete from  dept where  id = " + id);
//            preparedStatement.execute();
//            return true;
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return false;
//        }
//
//    }

    /**
     * 查询操作。根据查询条件  dname和loc进行模糊查询
     * @param param
     * @return
     */
    public List<Emp> findByParam(Emp param){
        String sql = "select * from emp where 1=1 ";
        //字符串的操作工具类：可以帮助判断字符串是否为null或''
        if(param != null){
            if(StringUtils.isNotEmpty(param.getEname())){
                sql += " and ename like '%"+param.getEname()+"%'";
            }
            if (param.getMgr()!=-1) {
                sql += " and mgr like '%" + param.getMgr() + "%'";
            }
//             if (param.getSalary()!=-1.00){
//                sql += " and salary like '%"+param.getSalary()+"%'";
//            }
        }

        //将查询结果进行重新封装，返回集合bean对象
        //创建一个返回结果集类型的空对象
        List<Emp> list = new ArrayList<Emp>();
        try {
            Connection connection = dataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Emp emp = new Emp();
                emp.setId(resultSet.getInt("id"));
                emp.setEname(resultSet.getString("ename"));
                emp.setMgr(resultSet.getInt("mgr"));
                emp.setJoindate(resultSet.getDate("joindate"));
                emp.setSalary(resultSet.getDouble("salary"));
                emp.setBonus(resultSet.getDouble("bonus"));
                emp.setIdept_id(resultSet.getInt("dept_id"));
                list.add(emp);
            }
//            return list;
        } catch (SQLException e) {
            e.printStackTrace();
//            return list;
        }
        return list;
    }


    /**
     * 根据主键ID查询对象信息
     * @param id
     * @return
     */
//    public Dept findById(int id){
//        try {
//            Connection connection = dataSource.getConnection();
//            PreparedStatement preparedStatement = connection.prepareStatement("select  * from dept where  id = " + id);
//            ResultSet resultSet = preparedStatement.executeQuery();//此时，数据返回的肯定只有一条或0条
//            Dept dept = new Dept();
//            boolean result = resultSet.next();
//            if (result){
//                dept.setId(resultSet.getInt("id"));
//                dept.setdName(resultSet.getString("dname"));
//                dept.setLoc(resultSet.getString("loc"));
//            }
//            return dept;
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return null;
//        }

//    }
}
